<?php

/**
 * Introduction: 客服管理
 * @author: 王龙
 * @date: 2016/4/21 09:42
 * @email: wanglong@cdlinglu.com
 */

namespace Admin\Controller\Basic;

use Common\Controller\Admin\CommonController;

class CustomServiceController extends CommonController
{

    /**
     * Introduction: 客服列表
     * @author: 王龙
     * @date: 2016/4/21 10:04
     * @email: wanglong@cdlinglu.com
     */
	public function lists(){

		$CustomService = D('Common/CustomService','Logic');

		$this->lists = $CustomService::getData('id>0');
		$this->display();
	}

	/**
     * Introduction: 新增客服
     * @author: 王龙
     * @date: 2016/4/21 10:04
     * @email: wanglong@cdlinglu.com
     */
	public function create(){

		$CustomService = D('Common/CustomService','Logic');

		if(I('post.')){

			$data['tel'] = I('post.tel','');
			$data['qq'] = I('post.qq','');
			$data['wechat'] = I('post.wechat','');
			if(empty($data['tel'])){
				$this->ajaxReturn(['code'=>300,'msg'=>'客服联系方式不能为空']);
			}

			$isMob="/^1[3-5,8]{1}[0-9]{9}$/";
	 		$isTel="/^([0-9]{3,4}-)?[0-9]{7,8}$/";

	 		if(!preg_match($isMob,$data['tel']) && !preg_match($isTel,$data['tel']))
	 			$this->ajaxReturn(['code'=>300,'msg'=>'客服联系方式格式有误']);

			if(empty($data['qq']))
				$this->ajaxReturn(['code'=>300,'msg'=>'客服qq不能为空']);

			if(!is_numeric($data['qq']))
				$this->ajaxReturn(['code'=>300,'msg'=>'客服qq格式有误']);

			if(empty($data['wechat']))
				$this->ajaxReturn(['code'=>300,'msg'=>'客服微信号不能为空']);

			if($data['qq'] && $data['tel'] && $data['wechat'])
				$this->ajaxReturn($CustomService::create($data));
		}


		$this->display();
	}

	/**
     * Introduction: 后台修改客服信息页面
     * @author: 王龙
     * @date: 2016/4/21 13:04
     * @email: wanglong@cdlinglu.com
     */
	public function update(){

		$CustomService = D('Common/CustomService','Logic');

		$id = intval(I('post.id',''));

		if(!$id)
			$this->ajaxReturn(['code'=>300,'msg'=>'参数有误！']);

		$where['id'] = array('eq',$id);

		$this->data = $CustomService::getData($where);

		$this->display();
	}

	/**
     * Introduction: 修改客服信息
     * @author: 王龙
     * @date: 2016/4/21 13:04
     * @email: wanglong@cdlinglu.com
     */
	public function save(){

		$CustomService = D('Common/CustomService','Logic');

		$id = intval(I('post.id',''));
		$data['tel'] = I('post.tel','');
		$data['qq'] = I('post.qq','');
		$data['wechat'] = I('post.wechat','');

		if(!$id)
			$this->ajaxReturn(['code'=>300,'msg'=>'参数有误！']);

		if(empty($data['tel'])){
			$this->ajaxReturn(['code'=>300,'msg'=>'客服联系方式不能为空']);
		}

		$isMob="/^1[3-5,8]{1}[0-9]{9}$/";
 		$isTel="/^([0-9]{3,4}-)?[0-9]{7,8}$/";

 		if(!preg_match($isMob,$data['tel']) && !preg_match($isTel,$data['tel']))
 			$this->ajaxReturn(['code'=>300,'msg'=>'客服联系方式格式有误']);

		if(empty($data['qq']))
			$this->ajaxReturn(['code'=>300,'msg'=>'客服qq不能为空']);

		if(!is_numeric($data['qq']))
			$this->ajaxReturn(['code'=>300,'msg'=>'客服qq格式有误']);

		if(empty($data['wechat']))
			$this->ajaxReturn(['code'=>300,'msg'=>'客服微信号不能为空']);

		$where['id'] = array('eq',$id);

		$this->ajaxReturn($CustomService::save($where,$data));
		
	}

	/**
     * Introduction: 删除客服信息
     * @author: 王龙
     * @date: 2016/4/21 13:04
     * @email: wanglong@cdlinglu.com
     */
	public function delete(){

		$CustomService = D('Common/CustomService','Logic');

		$id = intval(I('post.id',''));//客服ID

		if(!$id)
			$this->ajaxReturn(['code'=>300,'msg'=>'参数有误！']);

		$where['id'] = array('eq',$id);

		$this->ajaxReturn($CustomService::delete($where));
	}
}